package com.huige.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huige.pojo.Headline;
import com.huige.pojo.vo.PortalVo;
import com.huige.service.HeadlineService;
import com.huige.mapper.HeadlineMapper;
import com.huige.utils.JwtHelper;
import com.huige.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 92746
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2024-07-21 23:09:00
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{

    @Autowired
    private HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 首页数据查询
     *
     * 1.进行分页数据查询
     * 2.分页查询数据，拼接到result中
     *
     *    注意1：查询需要自定义语句  自定义mapper方法，携带分页
     *    注意2：返回结果，List<Map>
     * @param portalVo
     * @return
     */
    @Override
    public Result findNewsPage(PortalVo portalVo) {

        //page -》当前页数 页容量
        IPage<Map> page = new Page<>(portalVo.getPageNum(),portalVo.getPageSize());
        headlineMapper.selsectMyPage(page,portalVo);

        List<Map> records = page.getRecords();
        Map data=new HashMap();
        data.put("pageData",records);
        data.put("pageNum",page.getCurrent());
        data.put("pageSize",page.getSize());
        data.put("totalPage",page.getPages());
        data.put("totalSize",page.getTotal());

        Map pageInfo = new HashMap();
        pageInfo.put("pageInfo",data);

        return Result.ok(pageInfo);


    }

    /**
     * 根据id查询详情
     *
     * 2.查询对应的数据即可 【多表查询，头条和用户表，方法需要自定义 返回map即可】
     * 1.修改阅读量 +1 【Version乐观锁 当前数据对应的版本号】
     * @param hid
     * @return
     */
    @Override
    public Result showHeadlineDetail(Integer hid) {

        Map<String, Object> data = headlineMapper.queryDateilMap(hid);
        Map headelineMap = new HashMap<>();
        headelineMap.put("headline",data);

        //修改阅读量+1
        Headline headline = new Headline();
        headline.setHid((Integer) data.get("hid"));
        headline.setVersion((Integer)data.get("version"));
        //阅读量+1
        headline.setPageViews((Integer) data.get("pageViews")+1);
        headlineMapper.updateById(headline);

        return Result.ok(headelineMap);

    }

    /**
     * 发布头条方法
     * <p>
     * 1. 补全数据
     *
     * @param headline
     * @param token
     * @return
     */
    @Override
    public Result publish(Headline headline, String token) {

        //1.根据token查询用户id
        Long userId = jwtHelper.getUserId(token);

        //数据装配
        headline.setPublisher(userId.intValue());
        headline.setPageViews(0);
        headline.setCreateTime(new Date());
        headline.setUpdateTime(new Date());

        headlineMapper.insert(headline);

        return Result.ok(null);

    }

    /**
     * 头条回显
     *
     * @param hid
     * @return
     */
    @Override
    public Result findHeadlineByHid(Integer hid) {
        Headline headline = headlineMapper.selectById(hid);
        Map data = new HashMap();
        data.put("headline",headline);
        return Result.ok(data);
    }
}




